//
//  TravelGoTableCell.m
//  MiaoTu
//
//  Created by dllo on 16/9/15.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TravelGoTableCell.h"
#import "UIView+Frame.h"
#import "TravelGoCollectionCell.h"
#import "Topic.h"
#define kTravelGoCollectionCell @"TravelGoCollectionCellIdentifier"

@interface TravelGoTableCell () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) UICollectionViewFlowLayout *fl;

@end

@implementation TravelGoTableCell

- (void)setTopicArray:(NSArray<Topic *> *)topicArray {
    _topicArray = topicArray;
    [_collectionView reloadData];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUp];
    }
    return self;
}

- (void)setUp {
    self.fl = [[UICollectionViewFlowLayout alloc] init];
    _fl.minimumInteritemSpacing = 7;
    _fl.minimumLineSpacing = 7;
    _fl.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _fl.sectionInset = UIEdgeInsetsMake(0, 13, 0, 13);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_fl];
    [self.contentView addSubview:_collectionView];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.bounces = NO;
    [_collectionView registerNib:[UINib nibWithNibName:@"TravelGoCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kTravelGoCollectionCell];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _topicArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TravelGoCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kTravelGoCollectionCell forIndexPath:indexPath];
    cell.topic = _topicArray[indexPath.item];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(115, _collectionView.dk_height - 26);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.sendTidBlock(_topicArray[indexPath.item].Tid);
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _collectionView.frame = self.contentView.bounds;
}


- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
